package com.eian.boot.mybatis.core.page;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mybatisflex.core.paginate.Page;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @author alex.meng
 * @createTime 2023-12-29 18:09
 * @description 基础分页查询对象
 **/
@Data
public class PageQuery {
    /**
     * 当前记录起始索引 默认值
     */
    public static final int DEFAULT_PAGE_NUM = 1;
    /**
     * 每页显示记录数 默认值 默认查10条
     */
    public static final int DEFAULT_PAGE_SIZE = 10;

    /**
     * 当前页数
     */
    private Integer current;
    /**
     * 分页大小
     */
    private Integer pageSize;

    /**
     * 无参构造器，使用默认分页参数
     */
    public PageQuery() {
        this(DEFAULT_PAGE_NUM, DEFAULT_PAGE_SIZE);
    }

    /**
     * 带参构造器
     *
     * @param current  当前页数
     * @param pageSize 分页大小
     */
    public PageQuery(Integer current, Integer pageSize) {
        this.pageSize = pageSize;
        this.current = current;
    }

    /**
     * 构建分页对象
     */
    public <T> Page<T> buildPage() {
        int pageNum = ObjectUtils.isNotEmpty(getCurrent()) ? getCurrent() : DEFAULT_PAGE_NUM;
        Integer size = ObjectUtils.isNotEmpty(getPageSize()) ? getPageSize() : DEFAULT_PAGE_SIZE;
        if (pageNum <= 0) {
            pageNum = DEFAULT_PAGE_NUM;
        }
        return new Page<>(pageNum, size);
    }

    @JsonIgnore
    public Integer getFirstNum() {
        return (current - 1) * pageSize;
    }
}
